/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.barco.ledApi;

import com.ideal.Interface.DevInterface;
import com.ideal.log.Log;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 *
 * @author Huyu
 */
public class BcmCDev implements DevInterface {

    public BcmCDev() {

    }
    /*
     *实现接口
     */

    public List<Map> getDevInfo() {
        List<Map> list = new ArrayList<Map>();

        Log.logger.info("bcmc getDevInfo");
        //多ip、遍历led接口
        int ledCount = LedConfig.m_LedPortCount;
        for (int i = 0; i < ledCount; i++) {
            int dstPort = LedConfig.m_LedPortStart + i;
            Map<String, Object> map = new HashMap<String, Object>();
            String ledHomeUrl = LedConfig.ProPrefix + LedConfig.m_BcmcIp + ":" + String.valueOf(dstPort) + LedConfig.Api;//home页
            HttpBase httpbase = new HttpBase();
            Log.logger.info("request rul :" + ledHomeUrl);
            String HomeResStr = httpbase.RequestUrl(ledHomeUrl);
            Log.logger.info("finish request rul :");
            ParseJson(map, HomeResStr);
            Log.logger.info("finish ParseJson rul :");
            if(!map.isEmpty()){
                list.add(map);
            }
        }
            //单ip测试
//        Map<String, Object> map = new HashMap<String, Object>();
//        String ledHomeUrl = Config.ProPrefix + "192.168.1.210" + Config.Api;
//        HttpBase httpbase = new HttpBase();
//        String HomeResStr = httpbase.RequestUrl(ledHomeUrl);
//        System.out.println("ex " + HomeResStr);
//        ParseJson(map, HomeResStr);
//        list.add(map);
//
//        int a = map.size();

        //本地测试
//        for (int i = 0; i < 10; i++) {
//            Map<String, Object> map = new HashMap<String, Object>();
//            map.put("_ol_data_monitoring_temps_ambient", "37");
//            map.put("_ol_data_monitoring_temps_fmtrBoard", "22");
//            map.put("_ol_data_monitoring_temps_cpuBoard", "24");
// 
//            list.add(map);
//        }
        Log.logger.info("bcmc getDevInfo finish");
        return list;
    }

    public void ParseJson(Map<String, Object> map, String JsonStr) {
        try {
            JSONObject jo = new JSONObject(JsonStr);
            //获取返回数据
            JSONArray paraArray = jo.getJSONArray("params");
            int paralen = paraArray.length();

            for (int i = 0; i < paralen; i++) {
                JSONObject paraObj = paraArray.getJSONObject(i);
                //String value = paraObj..getString("value1");
//                String Name = paraObj.getString("name");
//                String state = paraObj.getString("state");
                Object obj = paraObj.get("name");
                Object obj1 = paraObj.get("state");
                Object obj2 = paraObj.get("seq");
                Object obj3 = "*";

                if (obj1.equals("STATE_VALID")) {
                    obj3 = paraObj.get("value");
                }

                String NameStr = obj.toString();
                NameStr = NameStr.replaceAll("/", "_");
              
                map.put(NameStr, obj3);
            }
        } catch (Exception ex) {
            Log.logger.error("error :" + ex.getMessage());
            Log.logger.error("the JsonStr str is  :" + JsonStr);
        }
    }

}
